using System.Collections;
using UnityEngine;

public class LevelController : MonoBehaviour
{
	public static LevelController instance;

	public int level;

	private float genPosX;

	private Coroutine co;

	private EnemySpawnProgress[] enemySpawnProgress = new EnemySpawnProgress[13]
	{
		new EnemySpawnProgress
		{
			kind = new int[4] { 0, 0, 0, 5 },
			amount = new float[4] { 3f, 3f, 2f, 1f },
			delay = new float[4] { 2f, 1.5f, -1f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[5] { 0, 0, 0, 0, 6 },
			amount = new float[5] { 3f, 5f, 5f, 1f, 1f },
			delay = new float[5] { 0.3f, 1f, 0.5f, -1f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[5] { 0, 0, 0, 0, 9 },
			amount = new float[5] { 5f, 7f, 9f, 1f, 1f },
			delay = new float[5] { 0.3f, 1f, 0.7f, -1f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[6] { 0, 0, 8, 0, 0, 9 },
			amount = new float[6] { 7f, 5f, 1f, 10f, 7f, 1f },
			delay = new float[6] { 0.7f, 0.5f, 5f, 0.7f, 0.5f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[6] { 1, 6, 0, 7, 0, 0 },
			amount = new float[6] { 7f, 1f, 7f, 1f, 10f, 1f },
			delay = new float[6] { 0.5f, 3f, 0.7f, 2f, 0.5f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[5] { 9, 1, 9, 1, 1 },
			amount = new float[5] { 1f, 7f, 1f, 5f, 1f },
			delay = new float[5] { 0.3f, 0.5f, 0.5f, 0.5f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[5] { 10, 1, 5, 0, 1 },
			amount = new float[5] { 1f, 7f, 1f, 10f, 1f },
			delay = new float[5] { 5f, 0.5f, 2f, 0.5f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[6] { 2, 7, 9, 0, 2, 2 },
			amount = new float[6] { 10f, 1f, 1f, 5f, 5f, 1f },
			delay = new float[6] { 0.3f, 1f, 5f, 1f, 1f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[5] { 2, 6, 10, 2, 2 },
			amount = new float[5] { 7f, 1f, 1f, 5f, 1f },
			delay = new float[5] { 0.3f, 1f, 5f, 1f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[7] { 5, 2, 6, 2, 9, 2, 2 },
			amount = new float[7] { 1f, 5f, 1f, 5f, 1f, 10f, 1f },
			delay = new float[7] { 3f, 0.3f, 5f, 0.5f, 8f, 0.5f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[7] { 7, 2, 9, 2, 8, 2, 2 },
			amount = new float[7] { 1f, 5f, 1f, 5f, 1f, 10f, 1f },
			delay = new float[7] { 3f, 0.3f, 5f, 0.3f, 8f, 0.5f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[7] { 8, 2, 10, 2, 7, 2, 2 },
			amount = new float[7] { 1f, 5f, 1f, 5f, 1f, 10f, 1f },
			delay = new float[7] { 3f, 0.3f, 5f, 0.3f, 8f, 0.5f, -1f }
		},
		new EnemySpawnProgress
		{
			kind = new int[8] { 5, 2, 9, 2, 8, 2, 10, 2 },
			amount = new float[8] { 1f, 5f, 1f, 5f, 1f, 5f, 1f, 5f },
			delay = new float[8] { 5f, 0.3f, 8f, 0.3f, 5f, 0.3f, 8f, 0.5f }
		}
	};

	private void SpawnEnemy(int num, float genPosX)
	{
		if (TouchEvent.instance.CO_PlayerAI != null)
		{
			num = 0;
		}
		Vector2 pos = new Vector2(genPosX, Random.Range(1f, 5f));
		switch (num)
		{
		case 0:
			Generator.instance.SpawnObj(0, pos);
			break;
		case 1:
			Generator.instance.SpawnObj(17, pos, new float[1]);
			break;
		case 2:
			Generator.instance.SpawnObj(17, pos, new float[1] { 1f });
			break;
		case 5:
			Generator.instance.SpawnObj(18, pos, new float[1] { 1f });
			break;
		case 6:
			Generator.instance.SpawnObj(18, pos, new float[1] { 2f });
			break;
		case 7:
			Generator.instance.SpawnObj(18, pos, new float[1] { 4f });
			break;
		case 8:
			Generator.instance.SpawnObj(18, pos, new float[1] { 3f });
			break;
		case 9:
			Generator.instance.SpawnObj(21, pos);
			break;
		case 10:
			Generator.instance.SpawnObj(22, pos);
			break;
		case 100:
			Generator.instance.SpawnObj(27, pos, new float[1] { 1f });
			break;
		}
	}

	private void Awake()
	{
		instance = this;
	}

	public void Init()
	{
		level = 1;
		SetEnemySpeedByLevel();
		CanvasList.instance.TXT_Level.text = level.ToString();
		if (co != null)
		{
			StopCoroutine(co);
		}
		co = StartCoroutine(IE_SpawnEnemies());
	}

	private IEnumerator IE_SpawnEnemies()
	{
		int enemyNumber = 0;
		yield return new WaitForSeconds(1f);
		if (PlayerPrefs.GetInt("firstPlay") == 0 && GM.instance.GO_BTN_Pause.activeSelf)
		{
			yield return new WaitForSeconds(0.2f);
			while (Player.instance.kindOfMotion != 1)
			{
				yield return null;
			}
			yield return new WaitForSeconds(1f);
		}
		while (Player.instance.hp > 0f)
		{
			yield return new WaitForSeconds(0.2f);
			for (int p = 0; p < enemySpawnProgress[level - 1].kind.Length; p++)
			{
				for (int i = 0; (float)i < enemySpawnProgress[level - 1].amount[p]; i++)
				{
					if (enemySpawnProgress[level - 1].kind[p] < 5)
					{
						enemyNumber = Random.Range(0, enemySpawnProgress[level - 1].kind[p] + 1);
						genPosX = ((Random.Range(0, 2) != 0) ? 27f : (-27f));
						if (enemyNumber > 0 && Random.Range(0, 10) > 0)
						{
							enemyNumber = 0;
						}
					}
					else
					{
						enemyNumber = enemySpawnProgress[level - 1].kind[p];
						genPosX = ((Random.Range(0, 2) != 0) ? 35f : (-35f));
					}
					genPosX += Cam.instance.tf.position.x;
					SpawnEnemy(enemyNumber, genPosX);
					if (enemySpawnProgress[level - 1].delay[p] == -1f)
					{
						while (!Calc.IsEmptyField() && Player.instance.hp > 0f)
						{
							yield return new WaitForFixedUpdate();
						}
					}
					else
					{
						yield return new WaitForSeconds(enemySpawnProgress[level - 1].delay[p]);
					}
					if (Player.instance.hp <= 0f)
					{
						yield break;
					}
				}
			}
			if (TouchEvent.instance.CO_PlayerAI != null)
			{
				if (level < 13)
				{
					level++;
				}
				continue;
			}
			yield return new WaitForSeconds(1f);
			if (level < 13)
			{
				level++;
			}
			SetEnemySpeedByLevel();
			CanvasList.instance.TXT_Level.text = level.ToString();
			yield return new WaitForSecondsRealtime(0.5f);
			if (PlayerPrefs.GetInt("firstPlay") == 1 && level == 2)
			{
				PlayerPrefs.SetInt("firstPlay", 2);
				yield return new WaitForSeconds(2f);
				GM.instance.GO_HowToContol[1].SetActive(true);
				float temp = Time.timeScale;
				Time.timeScale = 0f;
				while (GM.instance.GO_HowToContol[1].activeSelf)
				{
					yield return new WaitForSecondsRealtime(0.1f);
				}
				Time.timeScale = temp;
			}
		}
	}

	public void SetEnemySpeedByLevel()
	{
		if (level > 4)
		{
			GM.instance.enemySpeedTimeByLevel = (float)(level - 4) * 0.05f;
		}
		else
		{
			GM.instance.enemySpeedTimeByLevel = 0f;
		}
	}
}
